package cn.swust.service.serviceimpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.swust.model.DonorBean;
import cn.swust.service.DonorService;
import cn.swust.util.DBUtil;

public class DonorServiceImpl implements DonorService {

	public int addDonor(DonorBean donor) {
		int ret = 0;
		try{
		String insertSql = "insert into donor(donor_proname,donor_procount,donor_flag,donor_address" +
		",donor_tel,donor_remarks) values ('" + donor.getProName() + "'," + donor.getProCount() + "," + 
		donor.getFlag() + ",'" + donor.getAddress() + "','" + donor.getTel() + "','" + donor.getRemarks() + "')";
		Connection conn = DBUtil.getConnection();
		Statement state = conn.createStatement();
		state.execute(insertSql, Statement.RETURN_GENERATED_KEYS);
		ResultSet rs = state.getGeneratedKeys();
		while(rs.next())
			ret = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return ret;
	}

	public int delDonorById(int id) {
		String delSql = "delete from donor where donor_id = " + id;
		return DBUtil.updateSql(delSql);
	}

	public int delDonorByProName(String proName) {
		String delSql = "delete from donor where donor_proname = '" + proName + "'";
		return DBUtil.updateSql(delSql);
	}

	public int modDonor(int id, DonorBean donor) {
		String modSql = "update donor set donor_proname = '" + donor.getProName() + "',donor_procount = " +
		donor.getProCount() + ",donor_flag = " + donor.getFlag() + ",donor_address = '" + donor.getAddress() +
		"',donor_tel = '" + donor.getTel() + "',donor_remarks = '" + donor.getRemarks() + "' where donor_id = " + id;
		return DBUtil.updateSql(modSql);
	}

	public DonorBean queryById(int id) {
		String querySql = "select * from donor where donor_id = " + 2;
		ResultSet rs = DBUtil.querySql(querySql, DBUtil.creatStatement(DBUtil.getConnection()));
		DonorBean donor = new DonorBean();
		try {
			while (rs.next()) {
				donor.setId(rs.getInt("donor_id"));
				donor.setProName(rs.getString("donor_proname"));
				donor.setProCount(rs.getInt("donor_procount"));
				donor.setFlag(rs.getInt("donor_flag"));
				donor.setTel(rs.getString("donor_tel"));
				donor.setAddress(rs.getString("donor_address"));
				donor.setRemarks(rs.getString("donor_remarks"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return donor;
	}

	public DonorBean queryByProName(String proName) {
		String querySql = "select * from donor where donor_proname = '" + proName + "'";;
		ResultSet rs = DBUtil.querySql(querySql, DBUtil.creatStatement(DBUtil.getConnection()));
		DonorBean donor = new DonorBean();
		try {
			while (rs.next()) {
				donor.setId(rs.getInt("donor_id"));
				donor.setProName(rs.getString("donor_proname"));
				donor.setProCount(rs.getInt("donor_procount"));
				donor.setFlag(rs.getInt("donor_flag"));
				donor.setTel(rs.getString("donor_tel"));
				donor.setAddress(rs.getString("donor_address"));
				donor.setRemarks(rs.getString("donor_remarks"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return donor;
	}

}
